<template>
	<view class="login">

		<view class="login_box">
			<view class="username">
				<text>手机号</text>
				<input type="text" placeholder="请输入手机号" v-model="phone" />
			</view>
			<view class="password">
				<text>验证码</text>
				<view class="inputbox">
					<input type="text" placeholder="请输入验证码" v-model="code" />
					<text class="code" @click="SendCode" v-show="is_send">获取验证码</text>
					<text class="code" v-show="!is_send">{{time}}s</text>
				</view>
			</view>

			<button class="btn" @click="login" :loading="loading" :disabled="loading">
				登录
			</button>

		</view>
	</view>
</template>

<script>
	import {
		Login,
		sendCode
	} from "@/api/api.js"
	export default {
		data() {
			return {
				username: '',
				phone: '',
				password: '',
				loading: false,
				time: 60,
				timer: '',
				code: '',
				is_send: true,
			}
		},
		onLoad() {

		},
		methods: {
			SendCode() {
				this.is_send = false
				this.time = 60
				sendCode({
					phone: this.phone
				}).then(res => {
					uni.$showMsg(res.msg)
					if (res.code == 0) {
						this.timer = setInterval(() => {
							this.time--;
							if (this.time < 0) {
								clearInterval(this.timer)
								this.is_send = true
							}
						}, 1000)
					}
				})
			},
			login() {
				if (this.phone == '') {
					return uni.$showMsg('请输入手机号')
				}
				if (this.code == '') {
					return uni.$showMsg('请输入验证码')
				}
				this.loading = true
				Login({
					phone: this.phone,
					code: this.code
				}).then(res => {
					this.loading = false
					if (res.code == 0) {
						uni.setStorageSync('token', res.data.token)
						uni.navigateBack()
					} else {
						uni.$showMsg(res.message)
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.login {
		padding-top: 20px;
		font-size: 16px;
		/* #ifdef H5 */
		min-height: calc(100vh - 44px);
		/* #endif */
		min-height: 100vh;
		box-sizing: border-box;
		background: #050B18;

		.title {
			display: flex;
			justify-content: space-around;
			align-items: center;
			color: #fff;
			width: 50%;
			margin: 0 auto;

			view {
				display: flex;
				flex-direction: column;
				align-items: center;
			}

			text {
				display: block;
				width: 30px;
				height: 3px;
				background: #fff;
				margin-top: 3px;
			}
		}

		.active {
			font-size: 24px;
		}

		.login_box {
			background: #fff;
			width: 80%;
			padding: 5%;
			padding-top: 10%;
			border-radius: 8px;
			margin: 20px auto;

			.inputbox {
				position: relative;
			}

			text {
				margin: 10px 0;
				display: block;
				font-weight: bold;
				font-size: 14px;
			}

			.code {
				position: absolute;
				right: 10px;
				top: 50%;
				transform: translate(0, -50%);
				margin: 0;
				color: #050B18;
				font-weight: normal;
				width: 80px;
				text-align: center;
			}



			input {
				border: 1px solid #050B18;
				outline: 0;
				border-radius: 5px;
				height: 40px;
				padding-left: 15px;
				margin-top: 10px;
				font-size: 14px;
			}
		}

		.wxlogin {
			text-align: center;
			display: flex;
			align-items: center;
			justify-content: center;
			color: #ccc;

			image {
				width: 30px;
				margin: 0 10px;
			}
		}

		.tips {
			display: flex;
			justify-content: flex-end;
			font-size: 14px;
			align-items: center;

			text {
				font-weight: normal;
				color: #777;
			}

			.forget {
				color: $uni-text-color;
			}
		}


	}
</style>